package com.getpebble.android.common.core.util;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.PblPreferences.PrefKey;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class Music {
    private static final String TAG = Music.class.getSimpleName();
    private static Set<String> sNonMusicMediaAppSet;

    public static Set<String> getNonMusicMediaAppSet() {
        if (sNonMusicMediaAppSet == null) {
            sNonMusicMediaAppSet = new HashSet();
            sNonMusicMediaAppSet.add("com.google.android.apps.magazines");
            sNonMusicMediaAppSet.add("com.netflix.mediaclient");
            sNonMusicMediaAppSet.add("com.amazon.kindle");
            sNonMusicMediaAppSet.add("net.flixster.android");
            sNonMusicMediaAppSet.add("air.com.vudu.air.DownloaderTablet");
            sNonMusicMediaAppSet.add("com.espn.score_center");
            sNonMusicMediaAppSet.add("org.coursera.android");
        }
        return sNonMusicMediaAppSet;
    }

    public static List<ResolveInfo> getMediaReceivers(PackageManager packageManager, boolean filterHidden) {
        List<ResolveInfo> mediaReceivers = packageManager.queryBroadcastReceivers(new Intent("android.intent.action.MEDIA_BUTTON"), 96);
        Iterator<ResolveInfo> iterator = mediaReceivers.iterator();
        while (iterator.hasNext()) {
            ResolveInfo mediaReceiver = (ResolveInfo) iterator.next();
            if (getNonMusicMediaAppSet().contains(mediaReceiver.activityInfo.packageName)) {
                iterator.remove();
            }
            Trace.verbose(TAG, "mediaReceiver: " + getAppName(mediaReceiver, packageManager));
            Trace.verbose(TAG, "mediaReceiver: " + mediaReceiver.activityInfo.packageName);
        }
        return mediaReceivers;
    }

    public static String getAppName(ResolveInfo resolveInfo, PackageManager packageManager) {
        return resolveInfo.activityInfo.applicationInfo.loadLabel(packageManager).toString();
    }

    public static ResolveInfo getPreferredMusicPlayer(Context context, PblPreferences preferences) {
        String targetName = preferences.getStringData(PrefKey.MUSIC_TARGET, "");
        List<ResolveInfo> musicApps = getMediaReceivers(context.getPackageManager(), false);
        if (musicApps.isEmpty()) {
            Trace.warning(TAG, "No media receivers found");
            return null;
        } else if (targetName.isEmpty()) {
            Trace.warning(TAG, "Unknown music target, using fallback");
            return (ResolveInfo) musicApps.get(0);
        } else {
            for (ResolveInfo info : musicApps) {
                if (info.activityInfo.packageName.equals(targetName)) {
                    return info;
                }
            }
            Trace.error(TAG, "Failed to find package: " + targetName + "; using fallback");
            return (ResolveInfo) musicApps.get(0);
        }
    }
}
